#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <flgrLinearFFT.h>
#include <flgrCoreData.h>
#include <flgrCoreCopy.h>
#include <flgrCoreChannel.h>
#include <flgrCoreDataIO.h>
#include <flgrHistogram.h>
#include <flgrArith.h>
#include "../macro.h"


/*******************************************************************************************/
/******************************************* 1D TESTS **************************************/
/*******************************************************************************************/

int test_fft_1d_forward_cplx_fgFLOAT32(void) {

  fgFLOAT32 rawsrc[]={105.0, 115.0, 28.0,   41.0, 41.0, 48.0, 54.0, 57.0, 62.0,  70.0};

  fgFLOAT32 rawref[]={290.0, 331.0, 23.0 , 104.0, 53.0, 65.0, 70.0, 50.0, 88.0,  24.0};

  FLGR_Data1D *imin,*imref,*imfft;
  FLGR_Ret ret;

  imin  = flgr1d_create_signal(5,2,FLGR_FLOAT32);
  imref = flgr1d_create_signal(5,2,FLGR_FLOAT32);
  imfft = flgr1d_create_signal(5,2,FLGR_FLOAT32);

  flgr1d_import_raw_ptr(imin,rawsrc);
  flgr1d_import_raw_ptr(imref,rawref);

  ret = flgr1d_fft_forward_cplx(imfft,imin);

  flgr1d_arith_floor(imfft,imfft);

  check_and_display_data1d(imref,imfft,ret);

  flgr1d_destroy(imin);
  flgr1d_destroy(imref);
  flgr1d_destroy(imfft);

  return 1;

}

int test_fft_1d_forward_cplx_fgFLOAT64(void) {

  fgFLOAT64 rawsrc[]={105.0, 115.0, 28.0,   41.0, 41.0, 48.0, 54.0, 57.0, 62.0,  70.0};

  fgFLOAT64 rawref[]={290.0, 331.0, 23.0 , 104.0, 53.0, 65.0, 70.0, 50.0, 88.0,  24.0};

  FLGR_Data1D *imin,*imref,*imfft;
  FLGR_Ret ret;

  imin  = flgr1d_create_signal(5,2,FLGR_FLOAT64);
  imref = flgr1d_create_signal(5,2,FLGR_FLOAT64);
  imfft = flgr1d_create_signal(5,2,FLGR_FLOAT64);

  flgr1d_import_raw_ptr(imin,rawsrc);
  flgr1d_import_raw_ptr(imref,rawref);

  ret = flgr1d_fft_forward_cplx(imfft,imin);

  flgr1d_arith_floor(imfft,imfft);

  check_and_display_data1d(imref,imfft,ret);

  flgr1d_destroy(imin);
  flgr1d_destroy(imref);
  flgr1d_destroy(imfft);

  return 1;

}

int test_fft_1d_backward_cplx_fgFLOAT32(void) {

  fgFLOAT32 rawsrc[]={290.0, 331.0, 23.0 , 104.0, 53.0, 65.0, 70.0, 50.0, 88.0,  24.0};

  fgFLOAT32 rawref[]={524.0, 574.0,139.0,  205.0,205.0,240.0,270.0,285.0,309.0, 349.0};

  FLGR_Data1D *imout,*imref,*imfft;
  FLGR_Ret ret;

  imout  = flgr1d_create_signal(5,2,FLGR_FLOAT32);
  imref = flgr1d_create_signal(5,2,FLGR_FLOAT32);
  imfft = flgr1d_create_signal(5,2,FLGR_FLOAT32);

  flgr1d_import_raw_ptr(imfft,rawsrc);
  flgr1d_import_raw_ptr(imref,rawref);

  ret = flgr1d_fft_backward_cplx(imout,imfft);

  flgr1d_arith_floor(imout,imout);

  check_and_display_data1d(imref,imout,ret);

  flgr1d_destroy(imout);
  flgr1d_destroy(imref);
  flgr1d_destroy(imfft);

  return 1;

}

int test_fft_1d_backward_cplx_fgFLOAT64(void) {

  fgFLOAT64 rawsrc[]={290.0, 331.0, 23.0 , 104.0, 53.0, 65.0, 70.0, 50.0, 88.0,  24.0};

  fgFLOAT64 rawref[]={524.0, 574.0,139.0,  205.0,205.0,240.0,270.0,285.0,309.0, 349.0};

  FLGR_Data1D *imout,*imref,*imfft;
  FLGR_Ret ret;

  imout  = flgr1d_create_signal(5,2,FLGR_FLOAT64);
  imref = flgr1d_create_signal(5,2,FLGR_FLOAT64);
  imfft = flgr1d_create_signal(5,2,FLGR_FLOAT64);

  flgr1d_import_raw_ptr(imfft,rawsrc);
  flgr1d_import_raw_ptr(imref,rawref);

  ret = flgr1d_fft_backward_cplx(imout,imfft);

  flgr1d_arith_floor(imout,imout);

  check_and_display_data1d(imref,imout,ret);

  flgr1d_destroy(imout);
  flgr1d_destroy(imref);
  flgr1d_destroy(imfft);

  return 1;

}



int test_fft_1d_forward_real_fgFLOAT32(void) {

  fgFLOAT32 rawsrc[]={105.0, 115.0, 28.0,   41.0, 41.0, 48.0, 54.0, 57.0, 62.0,  70.0};

  fgFLOAT32 rawref[]={621.0,0.0, 127.0,28.0, 87.0,-45.0, 35.0,-60.0, -16.0,-52.0,-41.0,0.0};

  FLGR_Data1D *imin,*imref,*imfft;
  FLGR_Ret ret;

  imin  = flgr1d_create_signal(10,1,FLGR_FLOAT32);
  imref = flgr1d_create_signal(10/2+1,2,FLGR_FLOAT32);
  imfft = flgr1d_create_signal(10/2+1,2,FLGR_FLOAT32);

  flgr1d_import_raw_ptr(imin,rawsrc);
  flgr1d_import_raw_ptr(imref,rawref);

  ret = flgr1d_fft_forward_real(imfft,imin);

  flgr1d_arith_floor(imfft,imfft);

  check_and_display_data1d(imref,imfft,ret);

  flgr1d_destroy(imin);
  flgr1d_destroy(imref);
  flgr1d_destroy(imfft);

  return 1;

}



int test_fft_1d_forward_real_fgFLOAT64(void) {

  fgFLOAT64 rawsrc[]={105.0, 115.0, 28.0,   41.0, 41.0, 48.0, 54.0, 57.0, 62.0,  70.0};

  fgFLOAT64 rawref[]={621.0,0.0, 127.0,28.0, 87.0,-45.0, 35.0,-60.0, -16.0,-52.0,-41.0,0.0};

  FLGR_Data1D *imin,*imref,*imfft;
  FLGR_Ret ret;

  imin  = flgr1d_create_signal(10,1,FLGR_FLOAT64);
  imref = flgr1d_create_signal(10/2+1,2,FLGR_FLOAT64);
  imfft = flgr1d_create_signal(10/2+1,2,FLGR_FLOAT64);

  flgr1d_clear_all(imin);
  flgr1d_clear_all(imref);
  flgr1d_clear_all(imfft);

  flgr1d_import_raw_ptr(imin,rawsrc);
  flgr1d_import_raw_ptr(imref,rawref);

  ret = flgr1d_fft_forward_real(imfft,imin);

  flgr1d_arith_floor(imfft,imfft);

  check_and_display_data1d(imref,imfft,ret);

  flgr1d_destroy(imin);
  flgr1d_destroy(imref);
  flgr1d_destroy(imfft);

  return 1;

}



int test_fft_1d_backward_real_fgFLOAT32(void) {

  fgFLOAT32 rawsrc[]={621.0,0.0, 127.0,28.0, 87.0,-45.0, 35.0,-60.0, -16.0,-52.0,-41.0,0.0};

  fgFLOAT32 rawref[]={105.0, 115.0, 28.0,   41.0, 41.0, 48.0, 54.0, 57.0, 62.0,  70.0};


  FLGR_Data1D *imout,*imref,*imfft;
  FLGR_Ret ret;

  imout  = flgr1d_create_signal(10,1,FLGR_FLOAT32);
  imref = flgr1d_create_signal(10,1,FLGR_FLOAT32);
  imfft = flgr1d_create_signal(10/2+1,2,FLGR_FLOAT32);

  flgr1d_import_raw_ptr(imfft,rawsrc);
  flgr1d_import_raw_ptr(imref,rawref);

  ret = flgr1d_fft_backward_real(imout, imfft);

  flgr1d_histogram_normalize_str(imout,imout,"28","115");

  flgr1d_arith_ceil(imout,imout);

  check_and_display_data1d(imref,imout,ret);

  flgr1d_destroy(imout);
  flgr1d_destroy(imref);
  flgr1d_destroy(imfft);

  return 1;

}




int test_fft_1d_backward_real_fgFLOAT64(void) {

  fgFLOAT64 rawsrc[]={621.0,0.0, 127.0,28.0, 87.0,-45.0, 35.0,-60.0, -16.0,-52.0,-41.0,0.0};

  fgFLOAT64 rawref[]={105.0, 115.0, 28.0,   41.0, 41.0, 48.0, 54.0, 57.0, 62.0,  70.0};


  FLGR_Data1D *imout,*imref,*imfft;
  FLGR_Ret ret;

  imout  = flgr1d_create_signal(10,1,FLGR_FLOAT64);
  imref = flgr1d_create_signal(10,1,FLGR_FLOAT64);
  imfft = flgr1d_create_signal(10/2+1,2,FLGR_FLOAT64);

  flgr1d_import_raw_ptr(imfft,rawsrc);
  flgr1d_import_raw_ptr(imref,rawref);

  ret = flgr1d_fft_backward_real(imout, imfft);

  flgr1d_histogram_normalize_str(imout,imout,"28","115");

  flgr1d_arith_ceil(imout,imout);
  
  check_and_display_data1d(imref,imout,ret);

  flgr1d_destroy(imout);
  flgr1d_destroy(imref);
  flgr1d_destroy(imfft);

  return 1;

}


/*******************************************************************************************/
/******************************************* 2D TESTS **************************************/
/*******************************************************************************************/

int test_fft_2d_forward_cplx_fgFLOAT32(void) {

  fgFLOAT32 rawsrc[]={105.0,115.0, 28.0, 41.0, 41.0, 48.0, 54.0, 57.0, 62.0, 70.0,
		       72.0, 76.0, 75.0, 76.0, 76.0, 78.0, 75.0, 77.0, 78.0, 76.0,
		       75.0, 79.0, 77.0, 76.0, 77.0, 73.0, 71.0, 64.0, 62.0, 55.0,
		       50.0, 44.0, 42.0, 32.0,123.0,112.0,100.0, 88.0, 82.0, 73.0,
		       73.0, 76.0, 76.0, 81.0, 85.0, 86.0, 90.0, 90.0, 93.0, 92.0,
		       91.0, 96.0, 96.0,100.0, 96.0, 98.0, 98.0, 97.0,102.0, 99.0,
		       98.0, 96.0, 99.0,102.0, 98.0, 93.0,100.0, 99.0, 94.0, 96.0,
		       94.0, 90.0, 88.0, 87.0, 88.0, 88.0, 85.0, 87.0, 35.0,  5.0};

  fgFLOAT32 rawref[]={3204.0,3168.0, -35.0,-31.0, 116.0,171.0, 114.0,118.0, -110.0,-69.0,
		      -428.0,33.0, 183.0,191.0, 199.0,141.0, -31.0,94.0, 5.0,163.0,
		      23.0,-149.0, 113.0,80.0, -91.0,140.0, -17.0,96.0, 186.0,-184.0,
		      11.0,-81.0, -69.0,269.0, -25.0,167.0, 17.0,-64.0, -14.0,-82.0,
		      -88.0,10.0, 68.0,229.0, 13.0,-45.0, 8.0,-12.0, 217.0,116.0,
		      -105.0,32.0, -94.0,-43.0, -25.0,-3.0, 221.0,13.0, 221.0,-6.0,
		      -311.0,-5.0, -172.0,162.0, 123.0,-25.0, 146.0,-63.0, 48.0,104.0,
		      12.0,-362.0, 187.0,-26.0, 110.0,-29.0, 99.0,214.0, 156.0,149.0};

  FLGR_Data2D *imin,*imref,*imfft;
  FLGR_Ret ret;

  imin  = flgr2d_create_pixmap(8,5,2,FLGR_FLOAT32);
  imref = flgr2d_create_pixmap(8,5,2,FLGR_FLOAT32);
  imfft = flgr2d_create_pixmap(8,5,2,FLGR_FLOAT32);

  flgr2d_import_raw_ptr(imin,rawsrc);
  flgr2d_import_raw_ptr(imref,rawref);

  ret = flgr2d_fft_forward_cplx(imfft,imin);

  flgr2d_arith_floor(imfft,imfft);

  check_and_display_data2d(imref,imfft,ret);

  flgr2d_destroy(imin);
  flgr2d_destroy(imref);
  flgr2d_destroy(imfft);

  return 1;

}

int test_fft_2d_forward_cplx_fgFLOAT64(void) {

  fgFLOAT64 rawsrc[]={105.0,115.0, 28.0, 41.0, 41.0, 48.0, 54.0, 57.0, 62.0, 70.0,
		       72.0, 76.0, 75.0, 76.0, 76.0, 78.0, 75.0, 77.0, 78.0, 76.0,
		       75.0, 79.0, 77.0, 76.0, 77.0, 73.0, 71.0, 64.0, 62.0, 55.0,
		       50.0, 44.0, 42.0, 32.0,123.0,112.0,100.0, 88.0, 82.0, 73.0,
		       73.0, 76.0, 76.0, 81.0, 85.0, 86.0, 90.0, 90.0, 93.0, 92.0,
		       91.0, 96.0, 96.0,100.0, 96.0, 98.0, 98.0, 97.0,102.0, 99.0,
		       98.0, 96.0, 99.0,102.0, 98.0, 93.0,100.0, 99.0, 94.0, 96.0,
		       94.0, 90.0, 88.0, 87.0, 88.0, 88.0, 85.0, 87.0, 35.0,  5.0};

  fgFLOAT64 rawref[]={3204.0,3168.0, -35.0,-31.0, 116.0,171.0, 114.0,118.0, -110.0,-69.0,
		      -428.0,33.0, 183.0,191.0, 199.0,141.0, -31.0,94.0, 5.0,163.0,
		      23.0,-149.0, 113.0,80.0, -91.0,140.0, -17.0,96.0, 186.0,-184.0,
		      11.0,-81.0, -69.0,269.0, -25.0,167.0, 17.0,-64.0, -14.0,-82.0,
		      -88.0,10.0, 68.0,229.0, 13.0,-45.0, 8.0,-12.0, 217.0,116.0,
		      -105.0,32.0, -94.0,-43.0, -25.0,-3.0, 221.0,13.0, 221.0,-6.0,
		      -311.0,-5.0, -172.0,162.0, 123.0,-25.0, 146.0,-63.0, 48.0,104.0,
		      12.0,-362.0, 187.0,-26.0, 110.0,-29.0, 99.0,214.0, 156.0,149.0};

  FLGR_Data2D *imin,*imref,*imfft;
  FLGR_Ret ret;

  imin  = flgr2d_create_pixmap(8,5,2,FLGR_FLOAT64);
  imref = flgr2d_create_pixmap(8,5,2,FLGR_FLOAT64);
  imfft = flgr2d_create_pixmap(8,5,2,FLGR_FLOAT64);

  flgr2d_import_raw_ptr(imin,rawsrc);
  flgr2d_import_raw_ptr(imref,rawref);

  ret = flgr2d_fft_forward_cplx(imfft,imin);

  flgr2d_arith_floor(imfft,imfft);

  check_and_display_data2d(imref,imfft,ret);

  flgr2d_destroy(imin);
  flgr2d_destroy(imref);
  flgr2d_destroy(imfft);

  return 1;

}


int test_fft_2d_backward_cplx_fgFLOAT32(void) {

  fgFLOAT32 rawsrc[]={3204.0,3168.0, -35.0,-31.0, 116.0,171.0, 114.0,118.0, -110.0,-69.0,
		      -428.0,33.0, 183.0,191.0, 199.0,141.0, -31.0,94.0, 5.0,163.0,
		      23.0,-149.0, 113.0,80.0, -91.0,140.0, -17.0,96.0, 186.0,-184.0,
		      11.0,-81.0, -69.0,269.0, -25.0,167.0, 17.0,-64.0, -14.0,-82.0,
		      -88.0,10.0, 68.0,229.0, 13.0,-45.0, 8.0,-12.0, 217.0,116.0,
		      -105.0,32.0, -94.0,-43.0, -25.0,-3.0, 221.0,13.0, 221.0,-6.0,
		      -311.0,-5.0, -172.0,162.0, 123.0,-25.0, 146.0,-63.0, 48.0,104.0,
		      12.0,-362.0, 187.0,-26.0, 110.0,-29.0, 99.0,214.0, 156.0,149.0};

  fgFLOAT32 rawref[]={105.0,115.0, 28.0,42.0, 41.0,49.0, 54.0,58.0, 62.0,71.0,
		      72.0,77.0, 76.0,77.0, 76.0,79.0, 75.0,78.0, 78.0,77.0,
		      75.0,80.0, 77.0,77.0, 78.0,74.0, 71.0,65.0, 62.0,56.0,
		      50.0,45.0, 42.0,33.0, 123.0,113.0, 100.0,89.0, 82.0,74.0,
		      74.0,77.0, 76.0,82.0, 85.0,87.0, 90.0,91.0, 93.0,93.0,
		      91.0,97.0, 96.0,101.0, 96.0,99.0, 98.0,98.0, 102.0,100.0,
		      98.0,97.0, 99.0,103.0, 98.0,94.0, 100.0,100.0, 94.0,97.0,
		      94.0,91.0, 88.0,88.0, 88.0,89.0, 85.0,88.0, 35.0,5.0};

  FLGR_Data2D *imout,*imref,*imfft;
  FLGR_Ret ret;

  imout = flgr2d_create_pixmap(8,5,2,FLGR_FLOAT32);
  imref = flgr2d_create_pixmap(8,5,2,FLGR_FLOAT32);
  imfft = flgr2d_create_pixmap(8,5,2,FLGR_FLOAT32);

  flgr2d_import_raw_ptr(imfft,rawsrc);
  flgr2d_import_raw_ptr(imref,rawref);

  ret = flgr2d_fft_backward_cplx(imout,imfft);

  flgr2d_histogram_normalize_str(imout,imout,"28 5","123 115");
  flgr2d_arith_ceil(imout,imout);

  check_and_display_data2d(imref,imout,ret);

  flgr2d_destroy(imout);
  flgr2d_destroy(imref);
  flgr2d_destroy(imfft);

  return 1;

}


int test_fft_2d_backward_cplx_fgFLOAT64(void) {

  fgFLOAT64 rawsrc[]={3204.0,3168.0, -35.0,-31.0, 116.0,171.0, 114.0,118.0, -110.0,-69.0,
		      -428.0,33.0, 183.0,191.0, 199.0,141.0, -31.0,94.0, 5.0,163.0,
		      23.0,-149.0, 113.0,80.0, -91.0,140.0, -17.0,96.0, 186.0,-184.0,
		      11.0,-81.0, -69.0,269.0, -25.0,167.0, 17.0,-64.0, -14.0,-82.0,
		      -88.0,10.0, 68.0,229.0, 13.0,-45.0, 8.0,-12.0, 217.0,116.0,
		      -105.0,32.0, -94.0,-43.0, -25.0,-3.0, 221.0,13.0, 221.0,-6.0,
		      -311.0,-5.0, -172.0,162.0, 123.0,-25.0, 146.0,-63.0, 48.0,104.0,
		      12.0,-362.0, 187.0,-26.0, 110.0,-29.0, 99.0,214.0, 156.0,149.0};

  fgFLOAT64 rawref[]={105.0,115.0, 28.0,42.0, 41.0,49.0, 54.0,58.0, 62.0,71.0,
		      72.0,77.0, 76.0,77.0, 76.0,79.0, 75.0,78.0, 78.0,77.0,
		      75.0,80.0, 77.0,77.0, 78.0,74.0, 71.0,65.0, 62.0,56.0,
		      50.0,45.0, 42.0,33.0, 123.0,113.0, 100.0,89.0, 82.0,74.0,
		      74.0,77.0, 76.0,82.0, 85.0,87.0, 90.0,91.0, 93.0,93.0,
		      91.0,97.0, 96.0,101.0, 96.0,99.0, 98.0,98.0, 102.0,100.0,
		      98.0,97.0, 99.0,103.0, 98.0,94.0, 100.0,100.0, 94.0,97.0,
		      94.0,91.0, 88.0,88.0, 88.0,89.0, 85.0,88.0, 35.0,5.0};

  FLGR_Data2D *imout,*imref,*imfft;
  FLGR_Ret ret;

  imout = flgr2d_create_pixmap(8,5,2,FLGR_FLOAT64);
  imref = flgr2d_create_pixmap(8,5,2,FLGR_FLOAT64);
  imfft = flgr2d_create_pixmap(8,5,2,FLGR_FLOAT64);

  flgr2d_import_raw_ptr(imfft,rawsrc);
  flgr2d_import_raw_ptr(imref,rawref);

  ret = flgr2d_fft_backward_cplx(imout,imfft);

  flgr2d_histogram_normalize_str(imout,imout,"28 5","123 115");
  flgr2d_arith_ceil(imout,imout);

  check_and_display_data2d(imref,imout,ret);

  flgr2d_destroy(imout);
  flgr2d_destroy(imref);
  flgr2d_destroy(imfft);

  return 1;

}



int test_fft_2d_forward_real_fgFLOAT32(void) {

  fgFLOAT32 rawsrc[]={105.0,115.0, 28.0, 41.0, 41.0, 48.0, 54.0, 57.0, 62.0, 70.0,
		       72.0, 76.0, 75.0, 76.0, 76.0, 78.0, 75.0, 77.0, 78.0, 76.0,
		       75.0, 79.0, 77.0, 76.0, 77.0, 73.0, 71.0, 64.0, 62.0, 55.0,
		       50.0, 44.0, 42.0, 32.0,123.0,112.0,100.0, 88.0, 82.0, 73.0,
		       73.0, 76.0, 76.0, 81.0, 85.0, 86.0, 90.0, 90.0, 93.0, 92.0,
		       91.0, 96.0, 96.0,100.0, 96.0, 98.0, 98.0, 97.0,102.0, 99.0,
		       98.0, 96.0, 99.0,102.0, 98.0, 93.0,100.0, 99.0, 94.0, 96.0,
		       94.0, 90.0, 88.0, 87.0, 88.0, 88.0, 85.0, 87.0, 35.0,  5.0};

  fgFLOAT32 rawref[]={6372.0,0.0, -135.0,17.0, 159.0,-113.0, 71.0,-165.0, -11.0,-21.0, 36.0,0.0,
		      -372.0,418.0, 299.0,-91.0, 156.0,-222.0, 97.0,8.0, 94.0,-21.0, -44.0,-23.0,
		      -221.0,-239.0, 136.0,-93.0, 152.0,101.0, 108.0,4.0, -90.0,-22.0, -67.0,95.0,
		      -71.0,-114.0, 268.0,177.0, 243.0,28.0, -14.0,7.0, -28.0,49.0, -23.0,1.0,
		      -78.0,0.0, 326.0,14.0, -1.0,9.0, 22.0,42.0, -41.0,-99.0, -98.0,0.0,
		      -71.0,113.0, -81.0,88.0, 6.0,68.0, -46.0,-125.0, -116.0,-98.0, -23.0,-2.0,
		      -221.0,239.0, 103.0,323.0, -3.0,-116.0, -97.0,-103.0, 25.0,-69.0, -67.0,-95.0,
		      -372.0,-419.0, 98.0,-209.0, -17.0,-116.0, 142.0,-149.0, 40.0,-133.0, -44.0,22.0};

  FLGR_Data2D *imin,*imref,*imfft;
  FLGR_Ret ret;

  imin  = flgr2d_create_pixmap(8,10,1,FLGR_FLOAT32);
  imref = flgr2d_create_pixmap(8,10/2+1,2,FLGR_FLOAT32);
  imfft = flgr2d_create_pixmap(8,10/2+1,2,FLGR_FLOAT32);

  flgr2d_import_raw_ptr(imin,rawsrc);
  flgr2d_import_raw_ptr(imref,rawref);

  ret = flgr2d_fft_forward_real(imfft,imin);

  flgr2d_arith_floor(imfft,imfft);

  check_and_display_data2d(imref,imfft,ret);

  flgr2d_destroy(imin);
  flgr2d_destroy(imref);
  flgr2d_destroy(imfft);

  return 1;

}


int test_fft_2d_forward_real_fgFLOAT64(void) {

  fgFLOAT64 rawsrc[]={105.0,115.0, 28.0, 41.0, 41.0, 48.0, 54.0, 57.0, 62.0, 70.0,
		       72.0, 76.0, 75.0, 76.0, 76.0, 78.0, 75.0, 77.0, 78.0, 76.0,
		       75.0, 79.0, 77.0, 76.0, 77.0, 73.0, 71.0, 64.0, 62.0, 55.0,
		       50.0, 44.0, 42.0, 32.0,123.0,112.0,100.0, 88.0, 82.0, 73.0,
		       73.0, 76.0, 76.0, 81.0, 85.0, 86.0, 90.0, 90.0, 93.0, 92.0,
		       91.0, 96.0, 96.0,100.0, 96.0, 98.0, 98.0, 97.0,102.0, 99.0,
		       98.0, 96.0, 99.0,102.0, 98.0, 93.0,100.0, 99.0, 94.0, 96.0,
		       94.0, 90.0, 88.0, 87.0, 88.0, 88.0, 85.0, 87.0, 35.0,  5.0};

  fgFLOAT64 rawref[]={6372.0,0.0, -135.0,17.0, 159.0,-113.0, 71.0,-165.0, -11.0,-21.0, 36.0,0.0,
		      -372.0,418.0, 299.0,-91.0, 156.0,-222.0, 97.0,8.0, 94.0,-21.0, -44.0,-23.0,
		      -221.0,-239.0, 136.0,-93.0, 152.0,101.0, 108.0,4.0, -90.0,-22.0, -67.0,95.0,
		      -71.0,-114.0, 268.0,177.0, 243.0,28.0, -14.0,7.0, -28.0,49.0, -23.0,1.0,
		      -78.0,0.0, 326.0,14.0, -1.0,9.0, 22.0,42.0, -41.0,-99.0, -98.0,0.0,
		      -71.0,113.0, -81.0,88.0, 6.0,68.0, -46.0,-125.0, -116.0,-98.0, -23.0,-2.0,
		      -221.0,239.0, 103.0,323.0, -3.0,-116.0, -97.0,-103.0, 25.0,-69.0, -67.0,-95.0,
		      -372.0,-419.0, 98.0,-209.0, -17.0,-116.0, 142.0,-149.0, 40.0,-133.0, -44.0,22.0};

  FLGR_Data2D *imin,*imref,*imfft;
  FLGR_Ret ret;

  imin  = flgr2d_create_pixmap(8,10,1,FLGR_FLOAT64);
  imref = flgr2d_create_pixmap(8,10/2+1,2,FLGR_FLOAT64);
  imfft = flgr2d_create_pixmap(8,10/2+1,2,FLGR_FLOAT64);

  flgr2d_import_raw_ptr(imin,rawsrc);
  flgr2d_import_raw_ptr(imref,rawref);

  ret = flgr2d_fft_forward_real(imfft,imin);

  flgr2d_arith_floor(imfft,imfft);

  check_and_display_data2d(imref,imfft,ret);

  flgr2d_destroy(imin);
  flgr2d_destroy(imref);
  flgr2d_destroy(imfft);

  return 1;

}


int test_fft_2d_backward_real_fgFLOAT32(void) {

  fgFLOAT32 rawsrc[]={6372.0,0.0, -135.0,17.0, 159.0,-113.0, 71.0,-165.0, -11.0,-21.0, 36.0,0.0,
		      -372.0,418.0, 299.0,-91.0, 156.0,-222.0, 97.0,8.0, 94.0,-21.0, -44.0,-23.0,
		      -221.0,-239.0, 136.0,-93.0, 152.0,101.0, 108.0,4.0, -90.0,-22.0, -67.0,95.0,
		      -71.0,-114.0, 268.0,177.0, 243.0,28.0, -14.0,7.0, -28.0,49.0, -23.0,1.0,
		      -78.0,0.0, 326.0,14.0, -1.0,9.0, 22.0,42.0, -41.0,-99.0, -98.0,0.0,
		      -71.0,113.0, -81.0,88.0, 6.0,68.0, -46.0,-125.0, -116.0,-98.0, -23.0,-2.0,
		      -221.0,239.0, 103.0,323.0, -3.0,-116.0, -97.0,-103.0, 25.0,-69.0, -67.0,-95.0,
		      -372.0,-419.0, 98.0,-209.0, -17.0,-116.0, 142.0,-149.0, 40.0,-133.0, -44.0,22.0};

  fgFLOAT32 rawref[]={104.0,115.0, 28.0, 41.0, 41.0, 47.0, 54.0, 56.0, 62.0, 69.0,
		       71.0, 76.0, 75.0, 75.0, 75.0, 78.0, 74.0, 77.0, 78.0, 75.0,
		       74.0, 79.0, 77.0, 76.0, 76.0, 72.0, 71.0, 63.0, 61.0, 54.0,
		       49.0, 44.0, 42.0, 31.0,123.0,111.0,100.0, 87.0, 81.0, 73.0,
		       73.0, 75.0, 76.0, 80.0, 85.0, 86.0, 89.0, 89.0, 93.0, 92.0,
		       90.0, 96.0, 96.0, 99.0, 96.0, 98.0, 98.0, 97.0,102.0, 99.0,
		       98.0, 96.0, 98.0,102.0, 97.0, 93.0,100.0, 98.0, 94.0, 95.0,
		       94.0, 90.0, 88.0, 87.0, 87.0, 87.0, 85.0, 87.0, 34.0,  5.0};


  FLGR_Data2D *imout,*imout2,*imref,*imfft;
  FLGR_Ret ret;

  imout  = flgr2d_create_pixmap(8,10,1,FLGR_FLOAT32);
  imout2 = flgr2d_create_pixmap(8,10,1,FLGR_FLOAT32);
  imref  = flgr2d_create_pixmap(8,10,1,FLGR_FLOAT32);
  imfft  = flgr2d_create_pixmap(8,10/2+1,2,FLGR_FLOAT32);

  flgr2d_import_raw_ptr(imfft,rawsrc);
  flgr2d_import_raw_ptr(imref,rawref);

  ret = flgr2d_fft_backward_real(imout, imfft);

  flgr2d_histogram_normalize_str(imout,imout,"5","123");

  flgr2d_arith_floor(imout,imout);

  check_and_display_data2d(imref,imout,ret);

  flgr2d_destroy(imout);
  flgr2d_destroy(imout2);
  flgr2d_destroy(imref);
  flgr2d_destroy(imfft);

  return 1;

}


int test_fft_2d_backward_real_fgFLOAT64(void) {

  fgFLOAT64 rawsrc[]={6372.0,0.0, -135.0,17.0, 159.0,-113.0, 71.0,-165.0, -11.0,-21.0, 36.0,0.0,
		      -372.0,418.0, 299.0,-91.0, 156.0,-222.0, 97.0,8.0, 94.0,-21.0, -44.0,-23.0,
		      -221.0,-239.0, 136.0,-93.0, 152.0,101.0, 108.0,4.0, -90.0,-22.0, -67.0,95.0,
		      -71.0,-114.0, 268.0,177.0, 243.0,28.0, -14.0,7.0, -28.0,49.0, -23.0,1.0,
		      -78.0,0.0, 326.0,14.0, -1.0,9.0, 22.0,42.0, -41.0,-99.0, -98.0,0.0,
		      -71.0,113.0, -81.0,88.0, 6.0,68.0, -46.0,-125.0, -116.0,-98.0, -23.0,-2.0,
		      -221.0,239.0, 103.0,323.0, -3.0,-116.0, -97.0,-103.0, 25.0,-69.0, -67.0,-95.0,
		      -372.0,-419.0, 98.0,-209.0, -17.0,-116.0, 142.0,-149.0, 40.0,-133.0, -44.0,22.0};

  fgFLOAT64 rawref[]={104.0,115.0, 28.0, 41.0, 41.0, 47.0, 54.0, 56.0, 62.0, 69.0,
		       71.0, 76.0, 75.0, 75.0, 75.0, 78.0, 74.0, 77.0, 78.0, 75.0,
		       74.0, 79.0, 77.0, 76.0, 76.0, 72.0, 71.0, 63.0, 61.0, 54.0,
		       49.0, 44.0, 42.0, 31.0,123.0,111.0,100.0, 87.0, 81.0, 73.0,
		       73.0, 75.0, 76.0, 80.0, 85.0, 86.0, 89.0, 89.0, 93.0, 92.0,
		       90.0, 96.0, 96.0, 99.0, 96.0, 98.0, 98.0, 97.0,102.0, 99.0,
		       98.0, 96.0, 98.0,102.0, 97.0, 93.0,100.0, 98.0, 94.0, 95.0,
		       94.0, 90.0, 88.0, 87.0, 87.0, 87.0, 85.0, 87.0, 34.0,  5.0};


  FLGR_Data2D *imout,*imout2,*imref,*imfft;
  FLGR_Ret ret;

  imout  = flgr2d_create_pixmap(8,10,1,FLGR_FLOAT64);
  imout2 = flgr2d_create_pixmap(8,10,1,FLGR_FLOAT64);
  imref  = flgr2d_create_pixmap(8,10,1,FLGR_FLOAT64);
  imfft  = flgr2d_create_pixmap(8,10/2+1,2,FLGR_FLOAT64);

  flgr2d_import_raw_ptr(imfft,rawsrc);
  flgr2d_import_raw_ptr(imref,rawref);

  ret = flgr2d_fft_backward_real(imout, imfft);

  flgr2d_histogram_normalize_str(imout,imout,"5","123");
  flgr2d_arith_floor(imout,imout);

  check_and_display_data2d(imref,imout,ret);

  flgr2d_destroy(imout);
  flgr2d_destroy(imout2);
  flgr2d_destroy(imref);
  flgr2d_destroy(imfft);

  return 1;

}










#define FLGR_TEST_SWITCH_CORNER(dtype)				\
								\
  dtype rawsrc[]={105,115, 28, 41, 41, 48, 54, 57, 62, 70,	\
		   72, 76, 75, 76, 76, 78, 75, 77, 78, 76,	\
		   75, 79, 77, 76, 77, 73, 71, 64, 62, 55,	\
		   50, 44, 42, 32,123,112,100, 88, 82, 73,	\
		   73, 76, 76, 81, 85, 86, 90, 90, 93, 92,	\
		   91, 96, 96,100, 96, 98, 98, 97,102, 99,	\
		   98, 96, 99,102, 98, 93,100, 99, 94, 96,	\
		   94, 90, 88, 87, 88, 88, 85, 87, 35,  5};	\
								\
  dtype rawref[]={86, 90, 90, 93, 92, 73, 76, 76, 81, 85,	\
		  98, 98, 97, 102, 99, 91, 96, 96, 100, 96,	\
		  93, 100, 99, 94, 96, 98, 96, 99, 102, 98,	\
		  88, 85, 87, 35, 5, 94, 90, 88, 87, 88,	\
		  48, 54, 57, 62, 70, 105, 115, 28, 41, 41,	\
		  78, 75, 77, 78, 76, 72, 76, 75, 76, 76,	\
		  73, 71, 64, 62, 55, 75, 79, 77, 76, 77,	\
		  112, 100, 88, 82, 73, 50, 44, 42, 32, 123};	\
								\
								\
  FLGR_Data2D *imout,*imin,*imref;				\
  FLGR_Type type = flgr_get_type_from_string(#dtype);		\
  FLGR_Ret ret;							\
								\
  imin  = flgr2d_create_pixmap(8,10,1,type);			\
  imout = flgr2d_create_pixmap(8,10,1,type);			\
  imref  = flgr2d_create_pixmap(8,10,1,type);			\
								\
  flgr2d_import_raw_ptr(imin,rawsrc);				\
  flgr2d_import_raw_ptr(imref,rawref);				\
								\
  ret = flgr2d_fft_switch_corner(imout, imin);			\
								\
  check_and_display_data2d(imref,imout,ret);			\
								\
  flgr2d_destroy(imout);					\
  flgr2d_destroy(imin);						\
  flgr2d_destroy(imref);					\
								\
  return 1


int test_flgr2d_fft_switch_corner_fgUINT8(void) {
  FLGR_TEST_SWITCH_CORNER(fgUINT8);
}
int test_flgr2d_fft_switch_corner_fgUINT16(void) {
  FLGR_TEST_SWITCH_CORNER(fgUINT16);
}
int test_flgr2d_fft_switch_corner_fgUINT32(void) {
  FLGR_TEST_SWITCH_CORNER(fgUINT32);
}
int test_flgr2d_fft_switch_corner_fgINT8(void) {
  FLGR_TEST_SWITCH_CORNER(fgINT8);
}
int test_flgr2d_fft_switch_corner_fgINT16(void) {
  FLGR_TEST_SWITCH_CORNER(fgINT16);
}
int test_flgr2d_fft_switch_corner_fgINT32(void) {
  FLGR_TEST_SWITCH_CORNER(fgINT32);
}
int test_flgr2d_fft_switch_corner_fgFLOAT32(void) {
  FLGR_TEST_SWITCH_CORNER(fgFLOAT32);
}
int test_flgr2d_fft_switch_corner_fgFLOAT64(void) {
  FLGR_TEST_SWITCH_CORNER(fgFLOAT64);
}






















/* void module(FLGR_Data2D *imgdest, FLGR_Data2D *imgsrc) { */
/*   int i,j,k,l; */
/*   fgFLOAT64 *row_pointer_a; */
/*   fgFLOAT64 *row_pointer_d; */

/*   for(i=0 ; i<imgdest->size_y ; i++) { */
/*     row_pointer_a = (fgFLOAT64 *) imgsrc->array[i]; */
/*     row_pointer_d = (fgFLOAT64 *) imgdest->array[i]; */

/*     for(j=0,k=imgdest->size_x-1,l=imgdest->size_x/2 ; j<imgdest->size_x/2 ; j++,k--,l++) { */
/*       row_pointer_d[j]=sqrt(pow(row_pointer_a[j],2)+pow(row_pointer_a[k],2)); */
/*       row_pointer_d[k]=row_pointer_d[j]; */
	
/*     } */

/*   } */

  
/* } */

/* void test_Complex_Forward_Backward_FFT(FLGR_Data2D *imgdest_Re,FLGR_Data2D *imgdest_Im, */
/* 				       FLGR_Data2D *imgsrc_Re, FLGR_Data2D *imgsrc_Im) { */
/*   FLGR_FFTBuffer fft; */
/*   ImForwardComplexFFT_fgFLOAT64(&fft,imgsrc_Re,imgsrc_Im); */
/*   ImBackwardComplexFFT_fgFLOAT64(imgdest_Re,imgdest_Im,&fft); */
/* } */

/* void test_Real_Forward_Backward_FFT(FLGR_Data2D *imgdest, FLGR_Data2D *imgsrc) { */
/*   FLGR_FFTBuffer fft; */
/*   ImForwardFft_fgFLOAT64(&fft,imgsrc); */
/*   ImBackwardFft_fgFLOAT64(imgdest,&fft); */
/* } */


/* int main(int argc, char *argv[]) { */
/*   FLGR_Data2D *img,*imgout,*imgout2,*imgout3,*imgcoeff,*imgfloat64,*imgfloatIm64,*imgoutCorner; */
/*   //FFT_Buffer fft; */
/*   clock_t before, after; */
/*   int nbtime; */
/*   int i; */
/*   FLGR_FFTBuffer coeff; */
/*   FLGR_FFTBuffer fft,fft_filtered; */
/*   int size_filter; */
/*   fgFLOAT64 color=1; */
 
/*   if(argc<3) { */
/*     fprintf(stderr,"try this: %s <pgm file> <number of repetition for time measure>\n",argv[0]); */
/*     return -1; */
/*   } */
/*   sscanf(argv[2],"%d",&nbtime); */
  
  

/*   if((img=loadPgm(argv[1], "imin"))==NULL) exit(1); */
/*   imgfloat64 = createPixmap(img->size_y, img->size_x, FLGR_FLOAT64, "imgfloat64"); */
/*   imgfloatIm64 = createPixmap(img->size_y, img->size_x, FLGR_FLOAT64, "imgfloat64"); */
/*   imgout = createPixmap(img->size_y, img->size_x, FLGR_FLOAT64, "imgfft_Real"); */
/*   imgoutCorner = createPixmap(img->size_y, img->size_x, FLGR_FLOAT64, "imgfiltered"); */
/*   imgout2 = createPixmap(img->size_y, img->size_x, FLGR_FLOAT64, "imgfft_Complex_Re"); */
/*   imgout3 = createPixmap(img->size_y, img->size_x, FLGR_FLOAT64, "imgfft_Complex_Im"); */
/*   imgcoeff = createPixmap(img->size_y, img->size_x, FLGR_FLOAT64, "imgfiltercoeff"); */
 
/*   size_filter=img->size_x/2-10; */

/*   copyData2D(imgfloat64,img); */
/*   arithConstMult(imgfloatIm64,imgfloatIm64,"0"); */
  

/*   //setup filter coeff// */
/*   arithConstMult(imgcoeff,imgcoeff,"0"); */
/*   drawFilledRectangle(imgcoeff, */
/* 		      size_filter, */
/* 		      size_filter, */
/* 		      img->size_y-2*size_filter, */
/* 		      img->size_x-2*size_filter, */
/* 		      &color); */
/*   ImForwardFft_fgFLOAT64(&fft_filtered,imgcoeff); */
/*   ImForwardFft_fgFLOAT64(&coeff,imgcoeff); */

/*   ImForwardFft_fgFLOAT64(&fft,imgfloat64); */
/*   TEST_MACRO("Complex Mult",arithfftComplexMult_fgFLOAT64,&fft_filtered,&fft,&coeff); */
/*   ImBackwardFft_fgFLOAT64(imgout,&fft_filtered); */
/*   TEST_MACRO("Switch Corner",switchCorner,imgoutCorner, imgout); */

/*   TEST_MACRO("Real FFT and iFFT",test_Real_Forward_Backward_FFT,imgout3,imgcoeff); */
/*   TEST_MACRO("Complex FFT and iFFT",test_Complex_Forward_Backward_FFT,imgout2,imgout3,imgfloat64,imgfloatIm64); */

/*   savePgm(imgcoeff); */
/*   savePgm(imgoutCorner); */
/*   savePgm(imgout2); */
/*   //savePgm(imgout3); */
 
 
/*   destroyData2D(img); */
/*   destroyData2D(imgfloatIm64); */
/*   destroyData2D(imgfloat64); */
/*   destroyData2D(imgout); */
/*   destroyData2D(imgout2); */
/*   destroyData2D(imgout3); */
/*   destroyData2D(imgcoeff); */

/*   return 0; */
/* } */
